<?php
namespace Think\Template\TagLib;
use Think\Template\TagLib;
/**
 * 视图可视化编辑调用标签
 */
 class Label extends TagLib{
 // protected $tags 定义调用标签
    protected $tags = array(
        // '标签名'=>array('attr'=>'传入参数变量,传入参数变量','close'=>1) //close 是否闭合（0 或者1 默认为1，表示闭合）
        'label'=> array('attr'=> 'name','close'=>0),
    );
    
	
	public function _label($attr){
		$editLabel = I('get.label');  //获取get信息 确认是否编辑状态
		if($editLabel == 'show'){ 					//进入编辑状态
			//查询状态信息 是否有编辑权限
			$uid = $_SESSION['uid'];
			if($uid){					//如果存在用户id信息查询用户id 信息 判断是否登记权限够
				$user = M('user')->where("uid = '{$uid}'")->field('uisadmin')->find();
				if($user['uisadmin'] == 1){ 					//显示可编辑状态  判断是否有权限
					$labelName = $attr['name'];
					//根据标签名查询数据库 如果有信息 返回组合组合信息  否则显示空
					$labelData = M('label')->where("lname='{$labelName}'")->find();
					$labelStr = "";
					if($labelData){  		//组合返回数据
						if($labelData['ltype'] == 1){ 			//html代码类型
							$labelStr .= "<div class='label_main' label ='".$labelName."'  onclick=\"edit_label('".$labelName."','".$labelData['ltype']."');\" >";
							$labelStr .= "<div class='label_drage'></div>";
							$labelStr .= $labelData['lcode'];
							$labelStr .= "</div>";	
						}else if($labelData['ltype'] == 2){		//图片文字类型
							$url = stripos ($labelData['limage'], 'http://');
							if($url){
								$imgHref = $labelData['limage'];
							}else{
								$imgHref = "http://".HOST."/Uploads/label/big/".$labelData['limage'];
							}
							$labelStr .= "<div class='label_main' label ='".$labelName."'  onclick=\"edit_label('".$labelName."','".$labelData['ltype']."');\" >";
							$labelStr .= "<div class='label_main' label ='".$labelName."'  onclick=\"edit_label('".$labelName."','".$labelData['ltype']."');\" ></div>";
							$labelStr .= "<div class='label_drage'></div>";
							$labelStr .= "<a href='".$labelData['limgurl']."' ><img src='".$imgHref."' alt='' ></a>";
							$labelStr .= "</div>";
						}else if($labelData['ltype'] == 3){		//flash 类型
							
						}else if($labelData['ltype'] == 4){		//幻灯片类型
							
						}else if($labelData['ltype'] == 5){		//内容调用类型
							$data = json_decode($labelData['llabelcode'],true);		//json转回的php数组
							//查询选择的栏目信息 栏目id 模型  行数、列数  按照代码循环 返回字符串
							$limit = $data['llabelcode_row'] * $data['llabelcode_clow']?$data['llabelcode_row'] * $data['llabelcode_clow']:5;
							
							$sort = $data['sort']?$data['sort']:'desc';
							$cid = $data['category']?$data['category']:2;
							$strData  = M("defaultonmodel")->where("cid in(".$cid.") ")->limit($limit)->select();
							
							if($strData){
								$labelStr .= "<div class='label_main' label ='".$labelName."'  onclick=\"edit_label('".$labelName."','".$labelData['ltype']."');\" >";
								$labelStr .= "<div class='label_drage'></div>";
								foreach($strData as $i =>$v){
									/**
									 * 组合变量
									 */
									$url = U('Article/index','','',true)."?cid=".$v['cid'].'&aid='.$v['id'];
									$data['llabelcode_tit_length'] = $data['llabelcode_tit_length']?$data['llabelcode_tit_length']:50;
									$title = substr($v['atitle'],0,$data['llabelcode_tit_length']);
									
									$img = '';//图片
									$username = '';
									

									$tempStr = str_ireplace('"', '\'', $data['llabelcode']);		//单双引号替换
									$newTempStr = str_ireplace(array('\"','$url','$i','$title','$img','$id'),array("\'",$url,$i,$title,$img,$id) , $tempStr) ;  //替换变量
									
									//循环组合字符串
									$labelStr .= $newTempStr;	
													
								}
								$labelStr .= "</div>";	
							}
							
						}else if($labelData['ltype'] == 6){		//Mysql万能标签
							
						}
					}else{ //查询信息空时返回空标签						
						$labelStr .= "<div class='label_main' label ='".$labelName."'  onclick=\"lable_add('".$labelName."');\" >";
						$labelStr .= "<div class='label_drage'></div>";
						$labelStr .= "<table>";
						$labelStr .= "<tr>";
						$labelStr .= "<td>新标签,内容暂无:".$labelName."</td>";
						$labelStr .= "</tr>";
						$labelStr .= "</table>";
						$labelStr .= "</div>";
					}
					
				}else{
					echo "<script type='text/javascript'>alert('访问地址不存在');</script>";
				}
				
			}else{						//未登录跳转登陆页面
				echo "<script type='text/javascript'>location.href='../../';</script>";
			}
		}else{										//非编辑状态  查询分配数据
			
 					//显示可编辑状态  判断是否有权限
					$labelName = $attr['name'];
					//根据标签名查询数据库 如果有信息 返回组合组合信息  否则显示空
					$labelData = M('label')->where("lname='{$labelName}'")->find();
					$labelStr = "";
					if($labelData){  		//组合返回数据
						if($labelData['ltype'] == 1){ 			//html代码类型
							$labelStr .= $labelData['lcode'];
						}else if($labelData['ltype'] == 2){		//图片文字类型
							//匹配网址 如果是绝对路径就直接赋值  不是则组合路径
							$url = stripos ($labelData['limage'], 'http://');
							if($url){
								$imgHref = $labelData['limage'];
							}else{
								$imgHref = "http://".HOST."/Uploads/label/big/".$labelData['limage'];
							}
							$labelStr .= "<a href='".$labelData['limgurl']."' ><img src='".$imgHref."' alt='' ></a>";
						}else if($labelData['ltype'] == 3){		//flash 类型
							
						}else if($labelData['ltype'] == 4){		//幻灯片类型
							
						}else if($labelData['ltype'] == 5){		//内容调用类型
							$data = json_decode($labelData['llabelcode'],true); 		//json转回的php数组
							//查询选择的栏目信息 栏目id 模型  行数、列数  按照代码循环 返回字符串
							$limit = $data['llabelcode_row'] * $data['llabelcode_clow']?$data['llabelcode_row'] * $data['llabelcode_clow']:5;
							
							$sort = $data['sort']?$data['sort']:'desc';
							$cid = $data['category']?$data['category']:2;
							$strData  = M("defaultonmodel")->where("cid in(".$cid.") ")->limit($limit)->select();
							if($strData){
								foreach($strData as $i =>$v){
									/**
									 * 组合变量  
									 */
									$url = U('Article/index','','',true)."?cid=".$v['cid'].'&aid='.$v['id'];
									$data['llabelcode_tit_length'] = $data['llabelcode_tit_length']?$data['llabelcode_tit_length']:50;
									$title = substr($v['atitle'],0,$data['llabelcode_tit_length']);
									$pic = 'http://'.HOST.'/Uploads/model_images/thumb/100X100_'.$v['athum'];			//图片  需要根据选择的栏目个模型判断附加图片路径
								
									$username = '';
									$date_all  = date('Y-m-d H:i:s',$v['aaddtime']); 	 //默认时间格式  2015-08-11 20:16:15
									$date_y = date('Y',$v['aaddtime']);				//年份 格式 2015
									$date_m = date('m',$v['aaddtime']);				//月份  格式 08
									$date_d = date('d',$v['aaddtime']);				//日      格式02
									$date_h = date('H',$v['aaddtime']);				//时
									$date_i = date('i',$v['aaddtime']);				//分
									$date_s = date('s',$v['aaddtime']);				//秒
									$digest = substr($v['adigest'], 0,150);
									
									$tempStr = str_ireplace('"', '\'', $data['llabelcode']);		//单双引号替换
									$newTempStr = str_ireplace(array('\"','$url','$title','$pic','$id','$date_all','$date_y','$date_m','$date_d','$date_h','$date_i','$date_s','$digest','$i'),array("\'",$url,$title,$pic,$id,$date_all,$date_y,$date_m,$date_d,$date_h,$date_i,$date_s,$digest,$i) , $tempStr) ;  //替换变量
									
									//循环组合字符串
									$labelStr .= $newTempStr;				
								}
								
							}
							
						}else if($labelData['ltype'] == 6){		//Mysql万能标签
							
						}
					}else{ //查询信息空时返回空标签						
						$labelStr .= " ";
						
					}
					
		}
		//获取标签名
//		$lableName = $attr['name'];
//		$noinfo = "'<table class='".$attr['name']."' border='0' cellpadding='0' cellspacing='0'>  <tbody><tr>   <td>新标签,内容暂无:index_t1</td>  </tr></tbody></table>'";
		//返回值判断
//		$lableName = $attr['name']==$lableName?$noinfo:$lableName;		
//		p($labelStr);die;
		return $labelStr;
}
	

 }
